package com.niit.springbootshop.controller;

import com.niit.springbootshop.entity.User;
import com.niit.springbootshop.service.UserService;
import com.niit.springbootshop.service.impl.MailService;
import com.niit.springbootshop.tools.CodeUtil;
import com.niit.springbootshop.tools.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: springboot-demo
 * @description:
 * @author: hanliang
 * @create: 2021-05-08 14:15
 **/
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    MailService mailService;

    /**
     * 跳转到注册界面
     * @return
     */
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    /**
     * 跳转到登录界面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 实现注册
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody // 返回值就是json 不再是 界面名称
    public String register(User user, HttpSession session){

        // 检查验证码
        String sessionCode = (String)session.getAttribute("VCODE");

        if(!sessionCode.equalsIgnoreCase(user.getCaptcha())){
            return "998";
        }

        // 设置默认激活状态 未激活:0
        user.setState(0);
        // 设置激活码
        String code = CodeUtil.getActiveCode();
        user.setCode(code);
        // 密码加密
        user.setPassword(MD5Util.encode(user.getPassword()));

        int result = userService.insertSelective(user);

        if(result>0){
            // 发邮件
            Map<String,Object> data = new HashMap<>();
            data.put("code",code);
            mailService.sendTemplateMail(user.getEmail(),"NIIT商城用户激活邮件",data);
            return "100";
        }else{
            return "999";
        }
    }

    /**
     * 实现login
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody // 返回值就是json 不再是 界面名称
    public String login(User user, HttpSession session){

        // 检查验证码
        String sessionCode = (String)session.getAttribute("VCODE");
        if(!sessionCode.equalsIgnoreCase(user.getCaptcha())){
            return "998";
        }

        // 密码加密
        user.setPassword(MD5Util.encode(user.getPassword()));
        User result = userService.selectByUsernameAndPassword(user);

        if(result!=null){
            // 将用户信息保存到session
            session.setAttribute("USER",result);
            return "100";
        }else{
            return "999";
        }
    }

    /**
     * 激活用户
     * @return
     */
    @RequestMapping("/active")
    public String active(String code){
        int result =  userService.updateByCode(code);
        if(result>0){
            return "login";
        }else{
            return "500";
        }
    }

}
